package com.xyf.service.impl;

import com.xyf.mapper.AdminMapper;
import com.xyf.pojo.Admin;
import com.xyf.pojo.AdminExample;
import com.xyf.service.AdminService;
import com.xyf.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    AdminMapper adminMapper;

    @Override
    public Admin login(String name, String pwd){
//        根据传入的用户或到DB中查询相应用户对象
//        如果有条件，则一定要创建AdminExample的对象，用来封装
        AdminExample example = new AdminExample();
        /*
        * 如何添加条件
        * select * from admin where a_name = 'admin'
        * */
//        添加用户名a_name条件
        example.createCriteria().andANameEqualTo(name);

        List<Admin> list = adminMapper.selectByExample(example);
        if (list.size() >0 ){
            Admin admin = list.get(0);
            // 如果查询到用户对象，再进行密码的比对
           String miPwd = MD5Util.getMD5(pwd);
           if(miPwd.equals(admin.getaPass())){
               return admin;
           }
        }

        return null;
    }

}
